using System;
using System.Collections.Generic;
using System.Text;

namespace Blazing.Web.Controls.Webparts
{
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using Blazing.Web.Controls;
    using PageMenu = Blazing.BusinessObjects.Cms.Menu;
    using PageMenuItem = Blazing.BusinessObjects.Cms.MenuItem;
    using System.Diagnostics;
    using System.Web.Configuration;
    using Blazing.Web.Util;

    /// <summary>
    /// Exposes a cms menu control as a webpart
    /// </summary>
    [Themeable(true)]
    public class MenuWebpart : UserControl
    {
        CmsManager _manager = null;

        public CmsManager Manager
        {
            get
            {
                if (_manager == null)
                    _manager = Blazing.Util.CmsManagerHelper.FindManagerObject(Page.Controls);
                if (_manager == null)
                    throw new ApplicationException("There was no active CmsManager object found on the current page.");
                return _manager;
            }
        }

        private int _menuId = 1;
        public int MenuTypeId
        {
            get { return _menuId; }
            set { _menuId = value; }
        }

        private string _cssClass = "";
        public string CSSClass
        {
            get { return _cssClass; }
            set
            {
                _cssClass = value;
                this.EnsureChildControls();
                foreach (Control c in Controls)
                {
                    if (c.ID == "MenuControl")
                        ((System.Web.UI.WebControls.Menu)c).CssClass = value;
                }
            }
        }

        private Orientation _orientation = Orientation.Horizontal;
        public Orientation Orientation
        {
            get { return _orientation; }
            set
            {
                _orientation = value;
                this.EnsureChildControls();
                foreach (Control c in Controls)
                {
                    if(c.ID == "MenuControl")
                        ((System.Web.UI.WebControls.Menu)c).Orientation = value;
                }

            }
        }

        protected void UpdateItems()
        {
            this.EnsureChildControls();
            Menu mnu = ((Menu)FindControl("MenuControl"));
            mnu.Items.Clear();

            Debug.Assert(Manager.SiteId != null);
            Debug.Assert(Manager.PageId != null);

            PageMenu menu = CmsFacade.GetMenuBySiteType(Manager.SiteId.Value, MenuTypeId);
            Blazing.BusinessObjects.Cms.Contentpage page = CmsFacade.GetPageById(Manager.PageId.Value);

            Blazing.BusinessObjects.Cms.Contentpage rootPage = 
                Blazing.BusinessObjects.Cms.Contentpage.FindRootPage(page);

            if (menu == null || menu.ChildItems == null)
                return;

            int count = 0;
            foreach (PageMenuItem item in menu.ChildItems)
            {
                if (count++ > 50)
                    break;

                Blazing.BusinessObjects.Cms.Contentpage attachedPage = item.GetPage(rootPage.ID);
                if (attachedPage != null)
                {
                    string itemUrl = UrlFormatter.FormatUrl(attachedPage.Url);
                    MenuItem mitem = new MenuItem(item.Label, item.ID.ToString(), "", itemUrl);
                    mitem.ToolTip = item.Title;
                    mnu.Items.Add(mitem);

                    if (attachedPage.ID == page.ID)
                    {
                        mitem.Selected = true;
                    }

                    //load menu items
                    if (item.ChildItems.Count > 0)
                    {
                        foreach (PageMenuItem subitem in item.ChildItems)
                        {
                            Blazing.BusinessObjects.Cms.Contentpage subattachedPage = subitem.GetPage(rootPage.ID);
                            if (subattachedPage != null)
                            {
                                MenuItem sitem = new MenuItem(subitem.Label, subitem.ID.ToString(), "", subattachedPage.Url + (Context.Request.Url.Host.IndexOf("localhost") > -1 ? "Control.aspx" : ""));
                                sitem.ToolTip = subitem.Title;

                                if (subattachedPage.ID == page.ID)
                                {
                                    mitem.Selected = true;
                                    sitem.Selected = true;
                                }
                                mitem.ChildItems.Add(sitem);
                            }
                        }
                    }
                }
            }
        }

        protected override void CreateChildControls()
        {
            this.Controls.Clear();

            System.Web.UI.WebControls.Menu mnu = new System.Web.UI.WebControls.Menu();
            mnu.ID = "MenuControl";
            mnu.Orientation = _orientation;
            mnu.CssClass = _cssClass;

            this.Controls.Add(mnu);
        }

        public MenuWebpart()
        {
            PreRender += new EventHandler(MenuWebpart_PreRender);
        }

        void MenuWebpart_PreRender(object sender, EventArgs e)
        {
            if (Page != null)
                UpdateItems();
        }
    }


}
